package com.cy.platform;

import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.WindowManager;
import android.widget.TextView;

public class ScreenActivity extends AppCompatActivity {
    private TextView tvWidthPixels,tvHeightPixels,tvDensity,tvDensityDpi,tvScreenWidth,tvScreenHeight;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_screen);
        Utils.setDensity(getApplication(),this);
        tvWidthPixels = findViewById(R.id.tv_width_pixels);
        tvHeightPixels = findViewById(R.id.tv_height_pixels);
        tvDensity = findViewById(R.id.tv_density);
        tvDensityDpi = findViewById(R.id.tv_density_dpi);
        tvScreenWidth = findViewById(R.id.tv_screen_width);
        tvScreenHeight = findViewById(R.id.tv_screen_height);

        WindowManager wm = (WindowManager) this.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics dm = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(dm);
        int width = dm.widthPixels;         // 屏幕宽度（像素）
        int height = dm.heightPixels;       // 屏幕高度（像素）
        float density = dm.density;         // 屏幕密度（0.75 / 1.0 / 1.5）
        int densityDpi = dm.densityDpi;     // 屏幕密度dpi（120 / 160 / 240）
        // 屏幕宽度算法:屏幕宽度（像素）/屏幕密度
        int screenWidth = (int) (width / density);  // 屏幕宽度(dp)
        int screenHeight = (int) (height / density);// 屏幕高度(dp)

        tvWidthPixels.setText("屏幕宽度（像素）:"+width);
        tvHeightPixels.setText("屏幕高度（像素）:"+height);
        tvDensity.setText("屏幕密度:"+density);
        tvDensityDpi.setText("屏幕密度dpi:"+densityDpi);
        tvScreenWidth.setText("屏幕宽度(dp):"+screenWidth);
        tvScreenHeight.setText("屏幕高度(dp):"+screenHeight);
    }
}
